package com.hyt.it.ogt.pj.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyt.it.ogt.pj.model.entity.MarkDataCache;
import com.hyt.it.ogt.pj.model.entity.Project;
import com.hyt.it.ogt.pj.service.IMarkDataCacheService;
import com.hyt.it.ogt.pj.service.IProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 获取评卷数据定时任务
 */
@Component
@EnableScheduling
@Slf4j
public class GetMarkDataTask {

    @Autowired
    private IMarkDataCacheService iMarkDataCacheService;

    @Autowired
    private IProjectService iProjectService;

    @Value("${task.getMarkDataTask.enabled}")
    private boolean enabled;

    @Scheduled(fixedDelayString = "${task.getMarkDataTask.fixedDelay}")
//    @Transactional(rollbackFor = Exception.class)
    public void getMarkData() throws Exception {
        try {
            if (enabled) {
                //查询评卷数据缓存表，看是否有需要获取评卷数据的评卷任务
                List<MarkDataCache> markDataCacheList = iMarkDataCacheService.getCache();
                if (CollectionUtils.isEmpty(markDataCacheList)) {
                    //没有缓存数据，不做任何操作
                    return;
                } else {
                    for (MarkDataCache cache : markDataCacheList) {
                        //设置缓存为允许删除
                        cache.setAllowDelete(true);
                        iMarkDataCacheService.updateById(cache);
                        //判断评卷任务需要初始化还是需要增量
                        String projectId = cache.getId();
                        String officeId = cache.getOfficeId();
                        Project project = iProjectService.getById(projectId);
                        if (project != null && project.getIsInit()) {
                            //评卷项目已经初始化过，直接获取增量评卷数据
                            iProjectService.addProjectStudent(projectId, officeId);
                        } else {
                            //评卷项目未初始化，开始初始化评卷项目
                            iProjectService.initProject(projectId, officeId);
                        }
                    }
                    //评卷数据拉取完毕，删除评卷数据缓存表数据
                    for (MarkDataCache cache : markDataCacheList) {
                        QueryWrapper<MarkDataCache> queryWrapper = new QueryWrapper<MarkDataCache>();
                        queryWrapper.lambda()
                                .eq(MarkDataCache::getId, cache.getId())
                                .eq(MarkDataCache::getAllowDelete, true);
                        //只删除允许删除的缓存数据，防止初始化评卷数据或增量获取评卷数据的过程中缓存更新
                        iMarkDataCacheService.remove(queryWrapper);
                    }
                    log.info("评卷数据拉取完毕");
                }
            } else {
                //定时任务未启动，不做任何操作
                return;
            }
        } catch (Exception e) {
            log.error("定时任务getMarkData异常:{}", e.getMessage());
            throw e;
        }
    }

}
